home *** CD-ROM | disk | FTP | other *** search
/ Ultra Pack / UltraComputing Partner Applications.iso / SunLabs / tclTK / src / tcl7.4 / tests / open.test < prev    next >
Encoding:
Text File  |  1995-02-21  |  19.8 KB  |  630 lines

  1. # Commands covered:  open, close, gets, puts, read, seek, tell, eof, flush
  2. #
  3. # This file contains a collection of tests for one or more of the Tcl
  4. # built-in commands.  Sourcing this file into Tcl runs the tests and
  5. # generates output for errors.  No output means no errors were found.
  6. #
  7. # Copyright (c) 1991-1994 The Regents of the University of California.
  8. # Copyright (c) 1994-1995 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13. # @(#) open.test 1.27 95/02/20 16:35:10
  14.  
  15. if {[string compare test [info procs test]] == 1} then {source defs}
  16.  
  17. catch {exec rm -f test1 test2 test3}
  18. exec cat > test1 << "Two lines: this one\nand this one\n"
  19. exec cat > test2 << "line1\nline2\nline3\nline4\nline5\n"
  20.  
  21. test open-1.1 {open command (files only)} {
  22.     set f [open test1]
  23.     set x [gets $f]
  24.     close $f
  25.     set x
  26. } {Two lines: this one}
  27. test open-1.2 {open command (files only)} {
  28.     set f [open test1]
  29.     set f2 [open test2]
  30.     set f3 [open test1]
  31.     set f4 [open test1]
  32.     set x [list [gets $f] [gets $f2] [gets $f3] [gets $f4] \
  33.         [gets $f] [gets $f2]]
  34.     close $f
  35.     close $f2
  36.     close $f3
  37.     close $f4
  38.     set x
  39. } {{Two lines: this one} line1 {Two lines: this one} {Two lines: this one} {and this one} line2}
  40. test open-1.3 {open command (files only)} {
  41.     set f [open test3 w]
  42.     puts $f xyz
  43.     close $f
  44.     exec cat test3
  45. } "xyz"
  46. test open-1.4 {open command (files only)} {
  47.     set f [open test3 w]
  48.     puts $f xyz
  49.     close $f
  50.     set f [open test3 a]
  51.     puts $f 123
  52.     close $f
  53.     exec cat test3
  54. } "xyz\n123"
  55. test open-1.5 {open command (files only)} {
  56.     set f [open test3 w]
  57.     puts $f xyz\n123
  58.     close $f
  59.     set f [open test3 r+]
  60.     set x [gets $f]
  61.     seek $f 0 current
  62.     puts $f 456
  63.     close $f
  64.     list $x [exec cat test3]
  65. } "xyz {xyz
  66. 456}"
  67. test open-1.6 {open command (files only)} {
  68.     set f [open test3 w]
  69.     puts $f xyz\n123
  70.     close $f
  71.     set f [open test3 w+]
  72.     puts $f xyzzy
  73.     seek $f 2
  74.     set x [gets $f]
  75.     close $f
  76.     list $x [exec cat test3]
  77. } "zzy xyzzy"
  78. test open-1.7 {open command (files only)} {
  79.     set f [open test3 w]
  80.     puts $f xyz\n123
  81.     close $f
  82.     set f [open test3 a+]
  83.     puts $f xyzzy
  84.     flush $f
  85.     set x [tell $f]
  86.     seek $f -4 cur
  87.     set y [gets $f]
  88.     close $f
  89.     list $x [exec cat test3] $y
  90. } {14 {xyz
  91. 123
  92. xyzzy} zzy}
  93.  
  94. test open-2.1 {errors in open command} {
  95.     list [catch {open} msg] $msg
  96. } {1 {wrong # args: should be "open filename ?access? ?permissions?"}}
  97. test open-2.2 {errors in open command} {
  98.     list [catch {open a b c d} msg] $msg
  99. } {1 {wrong # args: should be "open filename ?access? ?permissions?"}}
  100. test open-2.3 {errors in open command} {
  101.     list [catch {open test1 x} msg] $msg
  102. } {1 {illegal access mode "x"}}
  103. test open-2.4 {errors in open command} {
  104.     list [catch {open test1 rw} msg] $msg
  105. } {1 {illegal access mode "rw"}}
  106. test open-2.5 {errors in open command} {
  107.     list [catch {open test1 r+1} msg] $msg
  108. } {1 {illegal access mode "r+1"}}
  109. test open-2.6 {errors in open command} {
  110.     string tolower [list [catch {open _non_existent_} msg] $msg $errorCode]
  111. } {1 {couldn't open "_non_existent_": no such file or directory} {posix enoent {no such file or directory}}}
  112.  
  113. if {![file exists ~/_test_] && [file writable ~]} {
  114.     test open-3.1 {tilde substitution in open} {
  115.     set f [open ~/_test_ w]
  116.     puts $f "Some text"
  117.     close $f
  118.     set x [file exists $env(HOME)/_test_]
  119.     exec rm -f $env(HOME)/_test_
  120.     set x
  121.     } 1
  122. }
  123. test open-3.2 {tilde substitution in open} {
  124.     set home $env(HOME)
  125.     unset env(HOME)
  126.     set x [list [catch {open ~/foo} msg] $msg]
  127.     set env(HOME) $home
  128.     set x
  129. } {1 {couldn't find HOME environment variable to expand "~/foo"}}
  130.  
  131. test open-4.1 {file id parsing errors} {
  132.     list [catch {eof gorp} msg] $msg $errorCode
  133. } {1 {bad file identifier "gorp"} NONE}
  134. test open-4.2 {file id parsing errors} {
  135.     list [catch {eof filex} msg] $msg
  136. } {1 {bad file identifier "filex"}}
  137. test open-4.3 {file id parsing errors} {
  138.     list [catch {eof file12a} msg] $msg
  139. } {1 {bad file identifier "file12a"}}
  140. test open-4.4 {file id parsing errors} {
  141.     list [catch {eof file123} msg] $msg
  142. } {1 {file "file123" isn't open}}
  143. test open-4.5 {file id parsing errors} {
  144.     list [catch {eof file1} msg] $msg
  145. } {0 0}
  146. test open-4.6 {file id parsing errors} {
  147.     list [catch {eof stdin} msg] $msg
  148. } {0 0}
  149. test open-4.7 {file id parsing errors} {
  150.     list [catch {eof stdout} msg] $msg
  151. } {0 0}
  152. test open-4.8 {file id parsing errors} {
  153.     list [catch {eof stderr} msg] $msg
  154. } {0 0}
  155. test open-4.9 {file id parsing errors} {
  156.     list [catch {eof stderr1} msg] $msg
  157. } {1 {bad file identifier "stderr1"}}
  158. set f [open test1]
  159. close $f
  160. set expect "1 {file \"$f\" isn't open}"
  161. test open-4.10 {file id parsing errors} {
  162.     list [catch {eof $f} msg] $msg
  163. } $expect
  164.  
  165. test open-5.1 {close command (files only)} {
  166.     list [catch {close} msg] $msg $errorCode
  167. } {1 {wrong # args: should be "close fileId"} NONE}
  168. test open-5.2 {close command (files only)} {
  169.     list [catch {close a b} msg] $msg $errorCode
  170. } {1 {wrong # args: should be "close fileId"} NONE}
  171. test open-5.3 {close command (files only)} {
  172.     list [catch {close gorp} msg] $msg $errorCode
  173. } {1 {bad file identifier "gorp"} NONE}
  174. test open-5.4 {close command (files only)} {
  175.     list [catch {close file4} msg] \
  176.         [string range $msg [string first {" } $msg] end] $errorCode
  177. } {1 {" isn't open} NONE}
  178.  
  179. test open-6.1 {puts command} {
  180.     list [catch {puts} msg] $msg $errorCode
  181. } {1 {wrong # args: should be "puts ?-nonewline? ?fileId? string"} NONE}
  182. test open-6.2 {puts command} {
  183.     list [catch {puts a b c d} msg] $msg $errorCode
  184. } {1 {wrong # args: should be "puts ?-nonewline? ?fileId? string"} NONE}
  185. test open-6.3 {puts command} {
  186.     list [catch {puts a b nonewlinx} msg] $msg $errorCode
  187. } {1 {bad argument "nonewlinx": should be "nonewline"} NONE}
  188. test open-6.4 {puts command} {
  189.     list [catch {puts gorp "New text"} msg] $msg $errorCode
  190. } {1 {bad file identifier "gorp"} NONE}
  191. test open-6.5 {puts command} {
  192.     set f [open test3]
  193.     set x [list [catch {puts $f "New text"} msg] \
  194.     [string range $msg [string first " " $msg] end] $errorCode]
  195.     close $f
  196.     set x
  197. } {1 { wasn't opened for writing} NONE}
  198. test open-6.6 {puts command} {
  199.     set f [open test3 w]
  200.     puts -nonewline $f "Text1"
  201.     puts -nonewline $f " Text 2"
  202.     puts $f " Text 3"
  203.     close $f
  204.     exec cat test3
  205. } {Text1 Text 2 Text 3}
  206.  
  207. test open-7.1 {gets command} {
  208.     list [catch {gets} msg] $msg $errorCode
  209. } {1 {wrong # args: should be "gets fileId ?varName?"} NONE}
  210. test open-7.2 {gets command} {
  211.     list [catch {gets a b c} msg] $msg $errorCode
  212. } {1 {wrong # args: should be "gets fileId ?varName?"} NONE}
  213. test open-7.3 {gets command} {
  214.     list [catch {gets a} msg] $msg $errorCode
  215. } {1 {bad file identifier "a"} NONE}
  216. test open-7.4 {gets command} {
  217.     set f [open test3 w]
  218.     set x [list [catch {gets $f} msg] \
  219.         [string range $msg [string first " " $msg] end] $errorCode]
  220.     close $f
  221.     set x
  222. } {1 { wasn't opened for reading} NONE}
  223. set f [open test3 w]
  224. puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  225. puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  226. puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  227. puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  228. puts $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  229. close $f
  230. test open-7.5 {gets command with long line} {
  231.     set f [open test3]
  232.     set x [gets $f]
  233.     close $f
  234.     set x
  235. } {abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  236. test open-7.6 {gets command with long line} {
  237.     set f [open test3]
  238.     set x [gets $f y]
  239.     close $f
  240.     list $x $y
  241. } {260 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ}
  242. test open-7.7 {gets command and end of file} {
  243.     set f [open test3 w]
  244.     puts -nonewline $f "Test1\nTest2"
  245.     close $f
  246.     set f [open test3]
  247.     set x {}
  248.     set y {}
  249.     lappend x [gets $f y] $y
  250.     set y {}
  251.     lappend x [gets $f y] $y
  252.     set y {}
  253.     lappend x [gets $f y] $y
  254.     close $f
  255.     set x
  256. } {5 Test1 5 Test2 -1 {}}
  257. set f [open test3 w]
  258. puts $f "Line 1"
  259. puts $f "Line 2"
  260. close $f
  261. test open-7.8 {gets command and bad variable} {
  262.     catch {unset x}
  263.     set x 24
  264.     set f [open test3 r]
  265.     set result [list [catch {gets $f x(0)} msg] $msg]
  266.     close $f
  267.     set result
  268. } {1 {can't set "x(0)": variable isn't array}}
  269.  
  270. test open-8.1 {read command} {
  271.     list [catch {read} msg] $msg $errorCode
  272. } {1 {wrong # args: should be "read fileId ?numBytes?" or "read ?-nonewline? fileId"} NONE}
  273. test open-8.2 {read command} {
  274.     list [catch {read -nonewline} msg] $msg $errorCode
  275. } {1 {bad file identifier "-nonewline"} NONE}
  276. test open-8.3 {read command} {
  277.     list [catch {read a b c} msg] $msg $errorCode
  278. } {1 {wrong # args: should be "read fileId ?numBytes?" or "read ?-nonewline? fileId"} NONE}
  279. test open-8.4 {read command} {
  280.     list [catch {read -nonew file4} msg] $msg $errorCode
  281. } {1 {bad file identifier "-nonew"} NONE}
  282. test open-8.5 {read command} {
  283.     list [catch {read stdin foo} msg] $msg $errorCode
  284. } {1 {bad argument "foo": should be "nonewline"} NONE}
  285. test open-8.6 {read command} {
  286.     list [catch {read file10} msg] $msg $errorCode
  287. } {1 {file "file10" isn't open} NONE}
  288. test open-8.7 {read command} {
  289.     set f [open test3 w]
  290.     set x [list [catch {read $f} msg] \
  291.         [string range $msg [string first " " $msg] end] $errorCode]
  292.     close $f
  293.     set x
  294. } {1 { wasn't opened for reading} NONE}
  295. test open-8.8 {read command} {
  296.     set f [open test1]
  297.     set x [list [catch {read $f 12z} msg] $msg $errorCode]
  298.     close $f
  299.     set x
  300. } {1 {expected integer but got "12z"} NONE}
  301. test open-8.9 {read command} {
  302.     set f [open test1]
  303.     set x [list [catch {read $f z} msg] $msg $errorCode]
  304.     close $f
  305.     set x
  306. } {1 {bad argument "z": should be "nonewline"} NONE}
  307. test open-8.10 {read command} {
  308.     set f [open test1]
  309.     set x [list [read $f 1] [read $f 2] [read $f]]
  310.     close $f
  311.     set x
  312. } {T wo { lines: this one
  313. and this one
  314. }}
  315. test open-8.11 {read command, with over-large count} {
  316.     set f [open test1]
  317.     set x [read $f 100]
  318.     close $f
  319.     set x
  320. } {Two lines: this one
  321. and this one
  322. }
  323. test open-8.12 {read command, -nonewline switch} {
  324.     set f [open test1]
  325.     set x [read -nonewline $f]
  326.     close $f
  327.     set x
  328. } {Two lines: this one
  329. and this one}
  330.  
  331. test open-9.1 {seek command} {
  332.     list [catch {seek foo} msg] $msg $errorCode
  333. } {1 {wrong # args: should be "seek fileId offset ?origin?"} NONE}
  334. test open-9.2 {seek command} {
  335.     list [catch {seek foo a b c} msg] $msg $errorCode
  336. } {1 {wrong # args: should be "seek fileId offset ?origin?"} NONE}
  337. test open-9.3 {seek command} {
  338.     list [catch {seek foo 0} msg] $msg $errorCode
  339. } {1 {bad file identifier "foo"} NONE}
  340. test open-9.4 {seek command} {
  341.     set f [open test2]
  342.     set x [list [catch {seek $f xyz} msg] $msg $errorCode]
  343.     close $f
  344.     set x
  345. } {1 {expected integer but got "xyz"} NONE}
  346. test open-9.5 {seek command} {
  347.     set f [open test2]
  348.     set x [list [catch {seek $f 100 gorp} msg] $msg $errorCode]
  349.     close $f
  350.     set x
  351. } {1 {bad origin "gorp": should be start, current, or end} NONE}
  352. set f [open test3 w]
  353. puts -nonewline $f "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  354. close $f
  355. test open-9.6 {seek command} {
  356.     set f [open test3]
  357.     set x [read $f 1]
  358.     seek $f 3
  359.     lappend x [read $f 1]
  360.     seek $f 0 start
  361.     lappend x [read $f 1]
  362.     seek $f 10 current
  363.     lappend x [read $f 1]
  364.     seek $f -2 end
  365.     lappend x [read $f 1]
  366.     seek $f 50 end
  367.     lappend x [read $f 1]
  368.     seek $f 1
  369.     lappend x [read $f 1]
  370.     close $f
  371.     set x
  372. } {a d a l Y {} b}
  373.  
  374. test open-10.1 {tell command} {
  375.     list [catch {tell} msg] $msg $errorCode
  376. } {1 {wrong # args: should be "tell fileId"} NONE}
  377. test open-10.2 {tell command} {
  378.     list [catch {tell a b} msg] $msg $errorCode
  379. } {1 {wrong # args: should be "tell fileId"} NONE}
  380. test open-10.3 {tell command} {
  381.     list [catch {tell a} msg] $msg $errorCode
  382. } {1 {bad file identifier "a"} NONE}
  383. test open-10.4 {tell command} {
  384.     set f [open test2]
  385.     set x [tell $f]
  386.     read $f 3
  387.     lappend x [tell $f]
  388.     seek $f 2
  389.     lappend x [tell $f]
  390.     seek $f 10 current
  391.     lappend x [tell $f]
  392.     seek $f 0 end
  393.     lappend x [tell $f]
  394.     close $f
  395.     set x
  396. } {0 3 2 12 30}
  397.  
  398. test open-11.1 {eof command} {
  399.     list [catch {eof} msg] $msg $errorCode
  400. } {1 {wrong # args: should be "eof fileId"} NONE}
  401. test open-11.2 {eof command} {
  402.     list [catch {eof a b} msg] $msg $errorCode
  403. } {1 {wrong # args: should be "eof fileId"} NONE}
  404. test open-11.3 {eof command} {
  405.     list [catch {eof file100} msg] $msg $errorCode
  406. } {1 {file "file100" isn't open} NONE}
  407. test open-11.4 {eof command} {
  408.     set f [open test1]
  409.     set x [eof $f]
  410.     lappend x [eof $f]
  411.     gets $f
  412.     lappend x [eof $f]
  413.     gets $f
  414.     lappend x [eof $f]
  415.     gets $f
  416.     lappend x [eof $f]
  417.     lappend x [eof $f]
  418.     close $f
  419.     set x
  420. } {0 0 0 0 1 1}
  421.  
  422. test open-12.1 {flush command} {
  423.     list [catch {flush} msg] $msg $errorCode
  424. } {1 {wrong # args: should be "flush fileId"} NONE}
  425. test open-12.2 {flush command} {
  426.     list [catch {flush a b} msg] $msg $errorCode
  427. } {1 {wrong # args: should be "flush fileId"} NONE}
  428. test open-12.3 {flush command} {
  429.     list [catch {flush a} msg] $msg $errorCode
  430. } {1 {bad file identifier "a"} NONE}
  431. test open-12.4 {flush command} {
  432.     set f [open test3]
  433.     set x [list [catch {flush $f} msg] \
  434.         [string range $msg [string first " " $msg] end] $errorCode]
  435.     close $f
  436.     set x
  437. } {1 { wasn't opened for writing} NONE}
  438. test open-12.5 {flush command} {
  439.     set f [open test3 w]
  440.     puts $f "Line 1"
  441.     puts $f "Line 2"
  442.     set f2 [open test3]
  443.     set x {}
  444.     lappend x [read -nonewline $f2]
  445.     close $f2
  446.     flush $f
  447.     set f2 [open test3]
  448.     lappend x [read -nonewline $f2]
  449.     close $f2
  450.     close $f
  451.     set x
  452. } {{} {Line 1
  453. Line 2}}
  454.  
  455. test open-13.1 {I/O to command pipelines} {
  456.     list [catch {open "| cat < test1 > test3" w} msg] $msg $errorCode
  457. } {1 {can't write input to command: standard input was redirected} NONE}
  458. test open-13.2 {I/O to command pipelines} {
  459.     list [catch {open "| echo > test3" r} msg] $msg $errorCode
  460. } {1 {can't read output from command: standard output was redirected} NONE}
  461. test open-13.3 {I/O to command pipelines} {
  462.     list [catch {open "| echo > test3" r+} msg] $msg $errorCode
  463. } {1 {can't read output from command: standard output was redirected} NONE}
  464. test open-13.4 {writing to command pipelines} {
  465.     exec rm test3
  466.     set f [open "| cat | cat > test3" w]
  467.     puts $f "Line 1"
  468.     puts $f "Line 2"
  469.     close $f
  470.     exec cat test3
  471. } {Line 1
  472. Line 2}
  473. test open-13.5 {reading from command pipelines} {
  474.     set f [open "| cat test2" r]
  475.     set x [list [gets $f] [gets $f] [gets $f]]
  476.     close $f
  477.     set x
  478. } {line1 line2 line3}
  479. test open-13.6 {both reading and writing from/to command pipelines} {
  480.     set f [open "| cat -u" r+]
  481.     puts $f "Line1"
  482.     flush $f
  483.     set x [gets $f]
  484.     close $f
  485.     set x
  486. } {Line1}
  487. test open-13.7 {errors in command pipelines} {
  488.     list [catch {open "|gorp"} msg] [string tolower $msg] \
  489.         [string tolower $errorCode]
  490. } {1 {couldn't execute "gorp": no such file or directory} {posix enoent {no such file or directory}}}
  491. test open-13.8 {errors in command pipelines} {
  492.     set f [open "|sleep 1" w]
  493.     exec sleep 3
  494.     puts $f output
  495.     set x [list [catch {flush $f} msg] [concat \
  496.         [string range $msg 0 [string first {"} $msg]] \
  497.         [string range $msg [string first : $msg] end]] $errorCode]
  498.     catch {close $f}
  499.     string tolower $x
  500. } {1 {error flushing " : broken pipe} {posix epipe {broken pipe}}}
  501.  
  502. test open-14.1 {POSIX open access modes: RDONLY} {
  503.     set f [open test1 RDONLY]
  504.     set x [list [gets $f] [catch {puts $f Test} msg] $msg]
  505.     close $f
  506.  
  507.     # The regsub is needed to avoid false errors if the file
  508.     # number varies from system to system.
  509.  
  510.     regsub {"file[0-9]*"} $x {"file"} x
  511.     set x
  512. } {{Two lines: this one} 1 {"file" wasn't opened for writing}}
  513. test open-14.2 {POSIX open access modes: RDONLY} {
  514.     catch {exec rm -f test3}
  515.     string tolower [list [catch {open test3 RDONLY} msg] $msg]
  516. } {1 {couldn't open "test3": no such file or directory}}
  517. test open-14.3 {POSIX open access modes: WRONLY} {
  518.     catch {exec rm -f test3}
  519.     string tolower [list [catch {open test3 WRONLY} msg] $msg]
  520. } {1 {couldn't open "test3": no such file or directory}}
  521. test open-14.4 {POSIX open access modes: WRONLY} {
  522.     exec echo xyzzy > test3
  523.     set f [open test3 WRONLY]
  524.     puts -nonewline $f "ab"
  525.     seek $f 0 current
  526.     set x [list [catch {gets $f} msg] $msg]
  527.     close $f
  528.     lappend x [exec cat test3]
  529.  
  530.     # The regsub is needed to avoid false errors if the file
  531.     # number varies from system to system.
  532.  
  533.     regsub {"file[0-9]*"} $x {"file"} x
  534.     set x
  535. } {1 {"file" wasn't opened for reading} abzzy}
  536. test open-14.5 {POSIX open access modes: RDWR} {
  537.     catch {exec rm -f test3}
  538.     string tolower [list [catch {open test3 RDWR} msg] $msg]
  539. } {1 {couldn't open "test3": no such file or directory}}
  540. test open-14.6 {POSIX open access modes: RDWR} {
  541.     exec echo xyzzy > test3
  542.     set f [open test3 RDWR]
  543.     puts -nonewline $f "ab"
  544.     seek $f 0 current
  545.     set x [gets $f]
  546.     close $f
  547.     lappend x [exec cat test3]
  548. } {zzy abzzy}
  549. test open-14.7 {POSIX open access modes: CREAT} {
  550.     catch {exec rm -f test3}
  551.     set f [open test3 {WRONLY CREAT} 0600]
  552.     file stat test3 stats
  553.     set x [format "0%o" [expr $stats(mode)&0777]]
  554.     puts $f "line 1"
  555.     close $f
  556.     lappend x [exec cat test3]
  557. } {0600 {line 1}}
  558. if $doNonPortableTests {
  559.     test open-14.8 {POSIX open access modes: CREAT} {
  560.     catch {exec rm -f test3}
  561.     set f [open test3 {WRONLY CREAT}]
  562.     close $f
  563.     file stat test3 stats
  564.     format "0%o" [expr $stats(mode)&0777]
  565.     } 0664
  566. }
  567. test open-14.9 {POSIX open access modes: CREAT} {
  568.     exec echo xyzzy > test3
  569.     set f [open test3 {WRONLY CREAT}]
  570.     puts -nonewline $f "ab"
  571.     close $f
  572.     exec cat test3
  573. } abzzy
  574. test open-14.10 {POSIX open access modes: APPEND} {
  575.     exec echo xyzzy > test3
  576.     set f [open test3 {WRONLY APPEND}]
  577.     puts $f "new line"
  578.     seek $f 0
  579.     puts $f "abc"
  580.     close $f
  581.     exec cat test3
  582. } {xyzzy
  583. new line
  584. abc}
  585. test open-14.11 {POSIX open access modes: EXCL} {
  586.     exec echo xyzzy > test3
  587.     set msg [list [catch {open test3 {WRONLY CREAT EXCL}} msg] $msg]
  588.     regsub " already " $msg " " msg
  589.     string tolower $msg
  590. } {1 {couldn't open "test3": file exists}}
  591. test open-14.12 {POSIX open access modes: EXCL} {
  592.     catch {exec rm -f test3}
  593.     set x [catch {set f [open test3 {WRONLY CREAT EXCL}]}]
  594.     puts $f "A test line"
  595.     close $f
  596.     lappend x [exec cat test3]
  597. } {0 {A test line}}
  598. test open-14.13 {POSIX open access modes: TRUNC} {
  599.     exec echo xyzzy > test3
  600.     set f [open test3 {WRONLY TRUNC}]
  601.     puts $f abc
  602.     close $f
  603.     exec cat test3
  604. } {abc}
  605. if $doNonPortableTests {
  606.     test open-14.14 {POSIX open access modes: NONBLOCK} {
  607.     catch {exec rm -f test3}
  608.     set f [open test3 {WRONLY NONBLOCK CREAT}]
  609.     puts $f "NONBLOCK test"
  610.     close $f
  611.     exec cat test3
  612.     } {NONBLOCK test}
  613. }
  614. test open-14.15 {POSIX open access modes: errors} {
  615.     concat [catch {open test3 "FOO \{BAR BAZ"} msg] $msg\n$errorInfo
  616. } "1 unmatched open brace in list
  617. unmatched open brace in list
  618.     while processing open access modes \"FOO {BAR BAZ\"
  619.     invoked from within
  620. \"open test3 \"FOO \\{BAR BAZ\"\""
  621. test open-14.16 {POSIX open access modes: errors} {
  622.     list [catch {open test3 {FOO BAR BAZ}} msg] $msg
  623. } {1 {invalid access mode "FOO": must be RDONLY, WRONLY, RDWR, APPEND, CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC}}
  624. test open-14.17 {POSIX open access modes: errors} {
  625.     list [catch {open test3 {TRUNC CREAT}} msg] $msg
  626. } {1 {access mode must include either RDONLY, WRONLY, or RDWR}}
  627.  
  628. catch {exec rm -f test1 test2 test3}
  629. concat {}
  630.